﻿using BL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class frmBanThanhPham : Form
    {
        BanThanhPhamBL banThanhPhamBL;
        BindingSource bsBanThanhPham = new BindingSource();
        DataTable dtBanThanhPham = null;
        DataSet dsBanThanhPham;

        public frmBanThanhPham()
        {
            InitializeComponent();
            banThanhPhamBL = new BanThanhPhamBL();
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            try
            {
                dgvBanThanhPham.AutoGenerateColumns = false;
                dsBanThanhPham = banThanhPhamBL.timKiemBanThanhPham(txtMaBanThanhPham.Text);
                bsBanThanhPham.DataSource = dsBanThanhPham.Tables[0];
                dgvBanThanhPham.DataSource = bsBanThanhPham;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }            
        }

        private void dgvBanThanhPham_RowValidated(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (dsBanThanhPham != null)
                {
                    bsBanThanhPham.EndEdit();
                    dgvBanThanhPham.EndEdit();
                    banThanhPhamBL.updateBanThanhPham(dsBanThanhPham.Tables[0]);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }             
        }

        private void btnDanhSach_Click(object sender, EventArgs e)
        {
            try
            {
                dgvBanThanhPham.AutoGenerateColumns = false;
                dsBanThanhPham = banThanhPhamBL.layDanhSachBanThanhPham();
                bsBanThanhPham.DataSource = dsBanThanhPham.Tables[0];
                dgvBanThanhPham.DataSource = bsBanThanhPham;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void frmBanThanhPham_Load(object sender, EventArgs e)
        {

        }
    }
}
